package com.zp.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.base.entity.ClinicIndex;
import com.zp.base.entity.ClinicSchedule;
import com.zp.base.entity.bo.ClinicIndexBo;
import com.zp.base.entity.vo.ClinicIndexVo;
import com.zp.base.mapper.ClinicIndexMapper;
import com.zp.base.mapper.ClinicScheduleMapper;
import com.zp.base.service.IClinicIndexService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 号别Service业务层处理
 *
 * @author zhang peng
 * @date 2023-01-15
 */
@RequiredArgsConstructor
@Service
public class ClinicIndexServiceImpl implements IClinicIndexService {

    private final ClinicIndexMapper baseMapper;

    private final ClinicScheduleMapper clinicScheduleMapper;

    /**
     * 查询号别
     */
    @Override
    public ClinicIndexVo queryById (String clinicIndexId) {
        return baseMapper.selectVoById(clinicIndexId);
    }

    /**
     * 查询号别列表(分页)
     */
    @Override
    public TableDataInfo<ClinicIndexVo> queryPageList (ClinicIndexBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ClinicIndex> lqw = buildQueryWrapper(bo);
        Page<ClinicIndexVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询号别列表(不分页)
     */
    @Override
    public List<ClinicIndexVo> queryList (ClinicIndexBo bo) {
        LambdaQueryWrapper<ClinicIndex> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ClinicIndex> buildQueryWrapper (ClinicIndexBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ClinicIndex> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ClinicIndex::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptId()), ClinicIndex::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getDoctorId()), ClinicIndex::getDoctorId, bo.getDoctorId());
        lqw.eq(StringUtils.isNotBlank(bo.getClinicItemId()), ClinicIndex::getClinicItemId, bo.getClinicItemId());
        lqw.eq(bo.getSort() != null, ClinicIndex::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getInputCode()), ClinicIndex::getInputCode, bo.getInputCode());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), ClinicIndex::getOrgId, bo.getOrgId());
        return lqw;
    }

    /**
     * 新增号别
     */
    @Override
    public Boolean insertByBo (ClinicIndexBo bo) {
        ClinicIndex add = BeanUtil.toBean(bo, ClinicIndex.class);
        validEntityBeforeSave(add);
        add.setDelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setClinicIndexId(add.getClinicIndexId());
        }
        return flag;
    }

    /**
     * 修改号别
     */
    @Override
    public Boolean updateByBo (ClinicIndexBo bo) {
        ClinicIndex update = BeanUtil.toBean(bo, ClinicIndex.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (ClinicIndex entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除号别
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        ids.forEach(item -> {
            List<ClinicSchedule> list = clinicScheduleMapper.selectList(Wrappers.<ClinicSchedule>lambdaQuery()
                    .eq(ClinicSchedule::getClinicIndexId, item)
                    .eq(ClinicSchedule::getDelFlag, "0"));
            if (CollectionUtil.isNotEmpty(list)) {
                throw new RuntimeException("号别已进行出诊排班,不能删除。");
            }
        });
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
